home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11527 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: HELP. How to convert '0x12' into FF char?
  5. Date: Sun, 24 Mar 96 22:56:04 GMT
  6. Organization: none
  7. Message-ID: <827708164snz@genesis.demon.co.uk>
  8. References: <4iissm$s76@nntp.ucs.ubc.ca> <4il52a$ma7@nntp.interaccess.com> <4itj1h$p8e@nntp.ucs.ubc.ca>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4itj1h$p8e@nntp.ucs.ubc.ca>
  15.            gordonw@unixg.ubc.ca "Gordon Wong" writes:
  16.  
  17. >void filtmoz() {
  18. >
  19. >  char c,d,e,s[5];
  20. >  c=fgetc(infile);
  21.  
  22. fgetc() returns an int. c,d,e must be declared as int.
  23.  
  24. >  while (c != EOF)  {   /* scan file byte by byte */
  25. >
  26. >    switch (c) {
  27. >        case '+' : c = ' '; fputc(c,outfile); break;
  28. >        case '&' : fputc('\n',outfile); break;
  29. >        case '%' : /* Any % encountered signals an ASCII Char */
  30. >                   {
  31. >                      d=fgetc(infile);
  32. >                      e=fgetc(infile);
  33.  
  34. Check d and e for EOF.
  35.  
  36. >                      sprintf(s, "0x%c%c", d, e);
  37.  
  38. You don't need the 0x here since the %x specifier below assumes a hexadecimal
  39. representation.
  40.  
  41. >                      sscanf(s,"%x",&c);
  42.  
  43. %x requires you to pass *scanf a pointer to an unsigned int.
  44.  
  45. You could replace the last 4 lines with simply:
  46.  
  47.                        fscanf(infile, "%2x", &u);
  48.  
  49. where u is defined as an unsigned int. fscanf also has a return value that
  50. should be tested - either the number of input items assigned or EOF on
  51. error.
  52.  
  53. -- 
  54. -----------------------------------------
  55. Lawrence Kirby | fred@genesis.demon.co.uk
  56. Wilts, England | 70734.126@compuserve.com
  57. -----------------------------------------
  58.